package com.yand.disk.demo.service.impl;

import com.yand.disk.demo.entity.FileInfoBean;
import com.yand.disk.demo.entity.TDownLink;
import com.yand.disk.demo.utils.DateUtils;
import com.yand.disk.demo.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author  yanzz
 *    下载文件校验接口实现类
 */
@Service
public class NetDiskCheckServiceImpl extends CheckServiceSuperImpl {

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 校验文件是否过期
     * @param fileInfoBean
     * @return
     */
    @Override
    public boolean checkFileIsexpire(TDownLink fileInfoBean) {
        Date createDate = fileInfoBean.getCreateTime();

        Date expireDate = DateUtils.dateAddSecond(createDate, Integer.parseInt(fileInfoBean.getExpiretime().toString()));

        return  DateUtils.isBeforeNowTime(expireDate);
    }


    /**
     * 校验下载次数是否超过次数
     * @param fileInfoBean
     * @return
     */
    @Override
    public boolean checkDownLoadCount(TDownLink fileInfoBean) {
        String isDownLaodCount = redisUtil.get("fileDownLoadCount:"+"admin:"+fileInfoBean.getId());

        if(isDownLaodCount != null){
            if(Integer.parseInt(isDownLaodCount) <= fileInfoBean.getDownloadcount()){
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }


    }

    /**
     * 校验文件下载是否需要提取码
     * @param fileInfoBean
     * @return
     */
    @Override
    public boolean checkFileIsValiCode(TDownLink fileInfoBean) {
        int result = Integer.parseInt(fileInfoBean.getIscode());
        if(result == 0){
            return true;
        }
        return false;
    }
}
